﻿using BCCommon;
using BCCommon.NPinyin;
using BCCommon.TMS.FreightEnums;
using BCData.TMS.FreightContact;
using BCDto.TMS.FreightContact;
using BCDto.Website.User;
using BCEntity.Common.EntityNameConst;
using BCEntity.TMS.FreightContact;
using BCService.Utils;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace BCService.TMS.FreightContactService
{
    public class FreightContactService : IFreightContactService
    {
        protected readonly string entityName = "FreightContactEntity";
        private readonly IFreightContactData freightContactData;
        private readonly IRedisService redisService;
        public FreightContactService(IFreightContactData freightContactData, IRedisService redisService)
        {
            this.freightContactData = freightContactData;
            this.redisService = redisService;
        }

        public FreightContactDto Add(UserDto userDto, FreightContactRequestDto freightContactRequestDto)
        {
           var freightContactExists = this.freightContactData.CheckFreightContactExistsAsync(userDto.UserId, freightContactRequestDto.ContactPhone, freightContactRequestDto.ContactType).GetAwaiter().GetResult();
            if (freightContactExists != null)
            {
                throw new ArgumentException("联系人信息已存在");
            }
            FreightContactEntity freightContactEntity = freightContactRequestDto.As<FreightContactEntity>();
            freightContactEntity.UserId = userDto.UserId;
            freightContactEntity.ContactInitials = Pinyin.GetInitials(freightContactRequestDto.ContactName);
            FreightContactEntity result = this.freightContactData.InsertAsync(freightContactEntity).GetAwaiter().GetResult();
            if (result != null)
            {
                string key = string.Format("{0}:UserId={1}", EntityNameConst.FreightContactEntity, userDto.UserId);
                this.redisService.Delete(key);
            }
            return result.As<FreightContactDto>();
        }

        public void Delete(UserDto userDto, long freightContactId)
        {
            var freightContactExists = this.freightContactData.CheckFreightContactExistsAsync(freightContactId, userDto.UserId).GetAwaiter().GetResult();
            if (freightContactExists == null)
            {
                throw new ArgumentException("联系人信息不存在");
            }
            this.freightContactData.DeleteAsync(freightContactId);
        }

        public IEnumerable<FreightContactDto> Get(long userId, FreightContactType? freightContactType)
        {
            string key = string.Format("{0}:UserId={1}:FreightContactType={2}", entityName, userId, freightContactType);
            var result = this.redisService.TryGet(key, () =>
            {
                Task<IEnumerable<FreightContactEntity>> list = this.freightContactData.GetAsync(userId, freightContactType);
                IEnumerable<FreightContactEntity> freightContactEntities = list.GetAwaiter().GetResult();
                var dtolist = freightContactEntities.As<IEnumerable<FreightContactDto>>();
                return dtolist;
            }, TimeSpan.FromMinutes(30));
            return result;
        }

        public FreightContactDto Get(long userId, string phone, FreightContactType freightContactType)
        {
            var task = this.freightContactData.GetAsync(userId, phone, freightContactType);
            var result = task.GetAwaiter().GetResult();
            return result.As<FreightContactDto>();
        }

        public FreightContactDto Update(UserDto userDto, long freightContactId, FreightContactRequestDto freightContactPutDto)
        {
            var freightContactExists = this.freightContactData.CheckFreightContactExistsAsync(freightContactId, userDto.UserId).GetAwaiter().GetResult();
            if (freightContactExists == null)
            {
                throw new ArgumentException("联系人信息不存在");
            }
            var exist = this.freightContactData.CheckFreightContactExistsAsync(userDto.UserId, freightContactPutDto.ContactPhone, freightContactPutDto.ContactType).GetAwaiter().GetResult();
            if (exist != null)
            {
                throw new ArgumentException("联系人信息已存在");
            }
            FreightContactEntity freightContactEntity = freightContactPutDto.As<FreightContactEntity>();
            FreightContactEntity result = this.freightContactData.UpdateAsync(freightContactEntity).GetAwaiter().GetResult();
            return result.As<FreightContactDto>();
        }
    }
}
